// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-01 21:58
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.BinaryAlgorithms;

import org.junit.jupiter.api.Test;

/**
 * 二进制的补数
 */
public class FindComplement {
    public int findComplement(int num) {
        int ans = 0;
        String tmp = Integer.toBinaryString(num);
        int len = Integer.SIZE - Integer.numberOfLeadingZeros(num);

        for (int i = len - 1; i >= 0; i--) {
            ans = ans << 1;
            ans += (((num >> i) & 1) == 1) ? 0 : 1;
            String tmp2 = Integer.toBinaryString(ans);
        }
        return ans;
    }

    @Test
    public void shout() {
        System.out.println(findComplement(5));
    }
}
